
using BootstrapBlazor.Components;
using Identity.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using WorkOrder.Interfaces;
using WorkOrder.Models;
using WorkSystem.Components.Components;
namespace WorkSystem.Components.Pages;

public partial class Recharge
{
    [Inject]
    [NotNull]
    private MyIdentityContext? AppContext { get; set; }
    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    [Inject]
    [NotNull]
    private IWorkOrderService workOrderService { get; set; }

 
    private 充值用户 Model = new();

    public class 充值用户
    {
        [ReadOnly(true)]
        public string? 用户名 { get; set; }

        [Required(ErrorMessage = "请输入充值金额")]
        [Range(0, 100000, ErrorMessage = "充值金额必须大于0")]
        public decimal 充值金额 { get; set; }

        public DateTime? 充值时间 { get; set; } = DateTime.Now;
    }
    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }
    private async Task<QueryData<用户账户余额表>> OnQueryAsync(QueryPageOptions options)
    {
        var menus = await workOrderService.GetRecharge();

        // 处理模糊查询
        if (options.Searches.Any())
        {
            menus = menus.Where(options.Searches.GetFilterFunc<用户账户余额表>(FilterLogic.Or)).ToList();
        }

        // 处理 Filter 高级搜索
        if (options.CustomerSearches.Any() || options.Filters.Any())
        {
            menus = menus.Where(options.CustomerSearches.Concat(options.Filters).GetFilterFunc<用户账户余额表>()).ToList();
        }

        return await Task.FromResult(new QueryData<用户账户余额表>
        {
            IsFiltered = true,
            IsSearch = true,
            IsSorted = true,
            Items = menus
        });
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        if (string.IsNullOrEmpty(AppContext.UserName))
        {
            await ToastService.Error("请先登录");
            return;
        }
    }

    private async Task OnRechargeUsersAsync(用户账户余额表 item)
    {
        var items = Utility.GenerateEditorItems<充值用户>();
        Model.用户名 = item.用户ID;

        var option = new EditDialogOption<充值用户>
        {
            Title = $"为用户充值 - 当前用户: {item.用户ID}",
            Model = Model,
            Items = items,
            ItemsPerRow = 2,
            RowType = RowType.Inline,
            LabelAlign = Alignment.Right,
            OnCloseAsync = () => Task.CompletedTask,
            OnEditAsync = async context =>
            {
                await workOrderService.RechargeUserBalanceAsync(
                    Model.用户名 ?? string.Empty,
                    Model.充值金额,
                    AppContext.UserName,
                    string.Empty,
                     $"{AppContext.UserName}在{Model.充值时间}充值{Model.充值金额}元给{Model.用户名}"
                );
                
        
                return await Task.FromResult(true);
            }
        };

        await DialogService.ShowEditDialog(option);

        StateHasChanged();
    }

    private async Task<bool> OnSaveAsync(用户账户余额表 model, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Add)
        {
            try
            {
                await workOrderService.RechargeUserBalanceAsync(
                    model.用户ID,
                    model.当前余额,
                    AppContext.UserName,
                    string.Empty,
                 $"{AppContext.UserName}在{model.最后更新时间}充值{model.当前余额}元给{model.用户ID}"
                );
            }
            catch (Exception ex)
            {
                await Task.FromException(ex);
            }

            return await Task.FromResult(true);
        }
        else
        {
            return true;
        }
    }


private async Task ShowAccountDetails(用户账户余额表 item)
{
    try
    {
        var flowData = await workOrderService.GetFlow(item.用户ID);
       
         
        var option = new DialogOption()
        {
            Title = $"用户账户余额详情 - 当前用户: {item.用户ID}",
            Size = Size.ExtraLarge, // 建议调整对话框尺寸
            Component = BootstrapDynamicComponent.CreateComponent<AccountDetails>( new Dictionary<string, object?>
                {
                [nameof(AccountDetails._accountDetails)] = flowData
                })
        };

        await DialogService.Show(option);
    }
    catch (Exception ex)
    {
        // 处理异常，例如显示错误提示
        await ToastService.Error("加载失败", ex.Message);
    }
}

}
